﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

//THAO TAC VOI NGUON DU LIEU
namespace DAL
{
    public class DataAccess
    {
        //tao ket noi
        public SqlConnection GetConnect()
        {
            return new SqlConnection("Data Source=.;Initial Catalog=QuanLyLichDay;Persist Security Info=True;User ID=sa;Password=123456");
        }
        //ham tra ve 1 bang
        public DataTable GetTable(string sql)
        {
            SqlConnection con = GetConnect();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
            return dt;
        }
        //ham thuc thi cau lenh
        public void ExcuteNonQuery(string sql)
        {
            SqlConnection con = GetConnect();
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            cmd.Dispose();
        }

        public string ExcuteScalar(string sql)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            string kq = cmd.ExecuteScalar().ToString();
            con.Close();
            cmd.Dispose();
            return kq;
        }

        public SqlDataReader ExecuteReader(string sql)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader reader = cmd.ExecuteReader();            
            return reader;
            con.Close();
        }

    }
}
